﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Common;
using ServiceCenterApp.Helper;
using Model;
using DevExpress.Utils;
using DevExpress.XtraEditors;

namespace ServiceCenterApp
{
    public partial class FormEntity : XtraForm, IChildForm
    {
        CategoryBLL bll = new CategoryBLL();
        KeyBLL keyBLL = new KeyBLL();
        EntityBLL entityBLL = new EntityBLL();

        List<Category> categoryList = new List<Category>();
        List<Key> keysList = new List<Key>();
        List<EntityView> entityList = new List<EntityView>();

        public FormEntity()
        {
            InitializeComponent();
        }

        private void FormCateogory_Load(object sender, EventArgs e)
        {
            WinformHelper.BindRepository(repositoryCategoryEditor, categoryList, "Name", "ID");
            WinformHelper.BindRepository(repositoryKeyEditor, keysList, "Name", "ID");
            loadData();
        }

        private void loadData()
        {
            categoryList.Clear();
            categoryList.AddRange(bll.GetAllCategory());
            this.repositoryCategoryEditor.DataSource = categoryList;
            keysList.Clear();
            keysList.AddRange(keyBLL.GetAllKey());
            this.repositoryKeyEditor.DataSource = keysList;
            entityList.Clear();
            entityList.AddRange(entityBLL.GetAllEntityView());
            this.entityViewBindingSource.DataSource = null;
            this.entityViewBindingSource.DataSource = entityList;
            //this.keysViewBindingSource.DataSource = null;
            //this.keysViewBindingSource.DataSource = keysList;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            object row = e.Row;
        }

        public void Save()
        {
            try
            {
                Program.MainWindow.ShowMessage("开始保存数据...");
                //keyBLL.Save(keysList);
                entityBLL.Save(entityList.Where(x => !string.IsNullOrEmpty(x.Content) && !string.IsNullOrEmpty(x.Title)).ToList());
                Program.MainWindow.ShowMessage("数据保存完成.");
            }
            catch (Exception ex)
            {
                Program.ShowError(ex);
            }
        }

        public void Delete()
        {
            try
            {
                Program.MainWindow.ShowMessage("开始删除数据...");
                entityBLL.Delete(entityList);
                this.BeginInvoke(new MethodInvoker(() =>
                {
                    for (int i = 0; i < entityList.Count; i++)
                    {
                        if (entityList[i].Selected)
                        {
                            entityList.RemoveAt(i); i--;
                            this.gridView1.RefreshData();
                        }
                    }
                }));
                Program.MainWindow.ShowMessage("删除数据完成.");
            }
            catch (Exception ex)
            {
                Program.ShowError(ex);
            }
        }

        public void RefreshData()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(() => { loadData(); }));
            }
            else
            {
                loadData();
            }
        }

        private void gridView1_ValidateRow_1(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            EntityView view = e.Row as EntityView;
            if (string.IsNullOrEmpty(view.Title))
            {
                e.Valid = false; e.ErrorText += "标题不能为空！";
            }
            else
            {
                if (view.Title.Length > 512)
                {
                    e.Valid = false; e.ErrorText += "标题长度不能超过512！";
                }
            }
            if (string.IsNullOrEmpty(view.Content))
            {
                e.Valid = false; e.ErrorText += "内容不能为空！";
            }
        }
    }
}
